import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'package:pull_to_refresh/pull_to_refresh.dart';



class TestSingleScrollPage extends StatefulWidget {
  const TestSingleScrollPage({super.key});

  @override
  State<TestSingleScrollPage> createState() => _TestSingleScrollPageState();
}

class _TestSingleScrollPageState extends State<TestSingleScrollPage> {
  List<String> data = [];
  final RefreshController _refreshController =
      RefreshController(initialRefresh: false);

  /// 注意在使用 SingleChildScrollView 嵌套ListView得时候，
  /// 如果使用Positioned，会导致ListView内容不显示

  Widget buildEmpty() {
    return SingleChildScrollView(
      // physics: const NeverScrollableScrollPhysics(),
      child: Stack(
        children: [
          Container(
            color: Colors.white,
            height: ScreenUtil().screenHeight,
          ),
          Image.asset(
            "images/ft_picture_chat_bg.webp",
            fit: BoxFit.fitWidth,
          ),
          const Positioned(
            top: 50,
            child: Text("没数据,请下拉刷新"),
          ),
          Container(
            margin: const EdgeInsets.only(top: 200),
            decoration: BoxDecoration(
              color: Colors.black45,
              borderRadius: BorderRadius.circular(35),
            ),
            child: ListView.builder(
              padding: const EdgeInsets.only(top: 20,left: 10,right: 10),
              shrinkWrap: true,
              physics: const NeverScrollableScrollPhysics(),
              itemBuilder: (context, index) {
                return Container(
                  height: 120,
                  color: Colors.blueGrey,
                  child: const Text("item"),
                );
              },
              itemCount: 10,
            ),
          )
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      body: refreshContainer(),
    );
  }

  Widget refreshContainer() {
    return SmartRefresher(
      controller: _refreshController,
      enablePullUp: true,
      footer: const ClassicFooter(),
      enablePullDown: true,
      onRefresh: () async {
        await Future.delayed(const Duration(milliseconds: 2000));
        if (mounted) {
          setState(() {
            // data.add("new");
          });
        }
        _refreshController.refreshCompleted();
      },
      onLoading: () async {
        await Future.delayed(const Duration(milliseconds: 2000));
        if (mounted) {
          setState(() {
            // data.add("new");
          });
        }
        _refreshController.loadComplete();
      },
      child: data.isEmpty
          ? buildEmpty()
          : ListView.builder(
              itemBuilder: (c, i) => Text(data[i]),
              itemCount: data.length,
              itemExtent: 100.0,
            ),
    );
  }

  Widget gest() {
    return SingleChildScrollView(
      child: Stack(
        children: <Widget>[
          Container(
            color: Colors.white,
          ),
          Image.asset(
            "images/ft_picture_chat_bg.webp",
            fit: BoxFit.fitWidth,
          ),
          Positioned(
            child: Expanded(
                child: ListView.builder(
              shrinkWrap: true,
              physics: const NeverScrollableScrollPhysics(),
              itemCount: 20,
              itemBuilder: (BuildContext context, int index) {
                return ListTile(
                  title: Text('Item $index'),
                );
              },
            )),
          ),
        ],
      ),
    );
  }
}
